var arr = [9, 5, 8, 4, 2, 6, 7, 3, 1],
    key = 8,
    min = 0,
    max = arr.length;

function orderBy(a, b)
{
    return (a == b) ? 0 : (a > b) ? 1 : -1;
};

arr.sort(orderBy);

while (true)
{
    var mid = Math.floor((min / 2) + (max / 2));

    if (key === arr[mid])
    {
        console.log(mid);
        break;
    }
    else if (key < arr[mid])
    {
        max = mid;
    }
    else
    {
        min = mid;
    }

    if (min + 1 === max)
    {
        console.log("Not found");
        break;
    }
}